<template>
  <div class="">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="">
                <el-button   @click="pulish" class="w150" type="success">添加账号</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" border style="width: 100%" header-row-class-name="bgd8">
                    
                    <el-table-column  label="序号" type="index" > </el-table-column>
                    <el-table-column prop="usercode" label="登录账号" > </el-table-column>
                    <el-table-column prop="username" label="用户名称" > </el-table-column>
                    <el-table-column  label="头像">
                        <template slot-scope="scope">
                            <img :src="httpImgUrl+scope.row.url" alt="" style="width: 50px;height: 50px">                          
                        </template>
                    </el-table-column>
                    <el-table-column prop="roleName" label="角色" > </el-table-column>
                    <el-table-column prop="createTime" label="开通日期"> 
                        <template slot-scope="scope">
                               <span>{{scope.row.createTime|formatDate}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作 " width="150px">
                        <template slot-scope="scope">
                            <el-button type="success" size="small"  @click="edit2(scope.row)">修改密码</el-button>
                            <el-button type="info" size="small"  @click="edit(scope.row)">编辑</el-button>
                            <el-button type="danger" size="small"  @click="del(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
        </el-table>
          <el-pagination background  layout="prev, pager, next" :total="totalCount" @current-change="currentChange" @prev-click="prevClick" @next-click="nextClick"> </el-pagination>
        <el-dialog title="添加账号" class="w750" :visible.sync="dialogTableVisible">
          <el-form ref="ruleForm"  :inline="true" :model="formData" class="msgform newstypeform" label-width="100px">
            <el-form-item label="登录账号" prop="usercode" class="formblock is-required">
              <el-input class="w300" v-model="formData.usercode"></el-input>
            </el-form-item>
            <el-form-item label="用户名称" prop="username" class="formblock is-required">
              <el-input class="w300" v-model="formData.username"></el-input>
            </el-form-item>
             <el-form-item label="角色" class="formblock is-required">
                <el-select class="w270"  v-model="formData.roleId">
                   <el-option
                      v-for="item in selectData"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="头像" class="formblock">
                <ul class="imglist">
                    <li @click="model='mainImg'">
                        <el-upload :show-file-list="false" :on-success="uploadSuccess" class="upload" :action="httpApi+'uploadfile/uploadFile'">
                            <img v-if="mainImg" :src="httpImgUrl+mainImg">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </li>
                </ul>         
            </el-form-item>  

           
            <el-button type="primary" class="w140" size="small" @click="onSubmit">确认</el-button>
          </el-form>
        </el-dialog>

          <el-dialog title="修改密码" class="w750" :visible.sync="dialogTableVisible2">
          <el-form ref="ruleForm" :inline="true" :model="formData2" class="msgform newstypeform" label-width="100px">
            <el-form-item label="新密码" prop="name" class="formblock is-required">
              <el-input class="w300" v-model="formData2.password"></el-input>
            </el-form-item>
            <el-button type="primary" class="w140" size="small" @click="onSubmit2">确认</el-button>
          </el-form>
        </el-dialog>
  </div>
</template>

<script>
// import ueditor from './ue.vue' //调用编辑器
export default {
  name: 'HelloWorld',
  data () {
    return {
      tableData: [{}],
      selected:'',
      curPage:1,
      totalCount:0,
      selectData:[],
      formInline:{},
      dialogTableVisible:false,
      dialogTableVisible2:false,
      formData:{
          usercode:'',
          username:'',
          roleId:'',
          password:''
        },
        formData2:{
          password:''
        },
     mainImg:''
    }
  },
  filters:{
    formatDate(date){
      date=new Date(date);
      var year=date.getFullYear();
      var month=date.getMonth()+1;
      var day=date.getDate();
      if(month<10){
        month='0'+month;
      }
      return year+'-'+month+'-'+day;
    }
  },
  methods:{
      edit(data){
          this.dialogTableVisible=true;
          this.formData.usercode = data.usercode;
          this.formData.username = data.username;
          this.formData.roleId = data.roleId;
          this.mainImg = data.url;
          this.formData.id=data.id;
      },
      edit2(data){
          this.dialogTableVisible2=true;
          this.formData2.id=data.id;
      },
      uploadSuccess(file){
          if(this.model=='mainImg'){
              this[this.model]=file.data[0];
          }else{
              this[this.model].push(file.data[0])
          }
      },
      onSubmit(){               
            var flag=true;      
            if(this.formData.usercode==null|| this.formData.usercode.trim()==""|| this.formData.username==null || this.formData.username.trim()=="" ||this.formData.roleId==null){
                 flag=false;
            } 
            this.formData.url =  this.mainImg; 
           if(flag){
              this.$http.post(this.$store.state.httpApi + (this.formData.id ?'sysUser/update':'sysUser/save'),this.formData).then(response =>{
              if(response.status == 200){
                this.$message.success(this.formData.id ? '修改成功' : '添加成功');
                this.dialogTableVisible=false;
                this.getRows();
              }else{
                console.log('接口返回失败!',response);
              }
              },response => {
                console.log('请求失败',response)
              })
           }else{
             this.$message.error('您还有必填项还没有填！');
           }        
      },

      onSubmit2(){ 
            var flag=true;      
            if(this.formData2.password==null|| this.formData2.password.trim()==""){
                 flag=false;
            } 
           if(flag){
              this.$http.post(this.$store.state.httpApi + ('sysUser/update'),this.formData2).then(response =>{
              if(response.status == 200){
                this.$message.success('修改成功');
                this.dialogTableVisible2=false;
                this.getRows();
              }else{
                console.log('接口返回失败!',response);
              }
              },response => {
                console.log('请求失败',response)
              })
           }else{
             this.$message.error('您还有必填项还没有填！');
           }        
      },
      pulish(){
           this.formData.usercode = null;
           this.formData.username = null;
           this.formData.roleId = null;
           this.mainImg = null;
           this.formData.id = null;
           this.dialogTableVisible=true;
          
      },
      del(data){
        this.$confirm('确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var id =data.id;
          var httpApi=this.$store.state.httpApi;
          this.$http.post(this.$store.state.httpApi + 'sysUser/del?id='+id).then(response =>{
            if(response.status == 200){
              this.$message.success('删除成功');
					    this.getRows();
            }else{
              console.log('接口返回失败!',response);
            }
          },response => {
            
            console.log('请求失败',response)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      currentChange(page){
          this.curPage = page;
          this.getRows();
      },
      //上一页
      prevClick(page){
          this.curPage = page;
          this.getRows();
      },
    //  下一页
      nextClick(page){
          this.curPage = page;
          this.getRows();
      },
       getRows(){
          this.$http.get(this.$store.state.httpApi + 'sysUser/page?currentPage='+this.curPage+'&pageNumber='+10).then(response =>{
          if(response.status == 200){
          this.tableData=response.data.data.pp;
          this.totalCount = response.data.data.page.totalPage*10;
          }else{
            console.log('接口返回失败!',response);
          }
          },response => {
            console.log('请求失败',response)
          })
       },
       getRowsOfRole(){
          this.$http.get(this.$store.state.httpApi + 'sysRole/all').then(response =>{
          if(response.status == 200){
          this.selectData=response.data.data;
          }else{
            console.log('接口返回失败!',response);
          }
          },response => {
            console.log('请求失败',response)
          })
       }
  },
   created(){
    this.getRows();
    this.getRowsOfRole();
  },
  computed: {
    httpApi(){
        return this.$store.state.httpApi
    },
    httpImgUrl(){
        return this.$store.state.httpImgUrl
    },
  },
  components: {
//使用编辑器
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
    .img{
        height:513px;
        width:917px;
        display:block;
        margin-bottom:20px;
    }
    .imglist{
        font-size:0;
    }
     .imglist li{
        width:100px;
        height:100px;
        display:inline-block;
        vertical-align: top;
        margin-right:10px;
        position:relative;
    }
    .imglist li img{
        width:100%;
        height:100%;
    }
     .upload{
        width:100%;
        height: 100%;
        box-sizing: border-box;
        border:1px solid #D9D9D9;
        font-size: 54px;
        text-align: center;
        // line-height: 200px;
        color: #D2D2D2;
        line-height:100px;
    }
    .upload img{
        width:100%;
        height: 100%;
    }
</style>
